﻿using SheepStorage.Domain.DTO.Output;
using SheepStorage.Domain.Entity;
using SheepStorage.Repository.Interface;
using SheepStorage.Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SheepStorage.Service.DataImp
{
    /// <summary>
    /// 品相测定服务实现
    /// </summary>
    public class SheepConditionService : ISheepConditionService
    {
        /// <summary>
        /// 品相测定仓储
        /// </summary>

        private readonly IBaseRepository<SheepCondition> repository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="repository">品相测定仓储</param>
        public SheepConditionService(IBaseRepository<SheepCondition> repository)
        {
            this.repository = repository;
        }

        /// <summary>
        /// 添加品相测定
        /// </summary>
        /// <param name="sheepCondition"></param>
        /// <returns></returns>
        public async Task<int> AddSheepCondition(SheepCondition sheepCondition)
        {
            return repository.Add(sheepCondition);
        }

        /// <summary>
        /// 批量删除品相测定
        /// </summary>
        /// <param name="SheepConditionIds"></param>
        /// <returns></returns>
        public int DelRangeSheepCondition(List<int> SheepConditionIds)
        {
            foreach (var item in SheepConditionIds)
            {
                var list = repository.GetInfoBy(item);
                list.IsDel = 1;
                repository.Update(list);
            }
            return 1;
        }

        /// <summary>
        /// 删除品相测定
        /// </summary>
        /// <param name="SheepConditionId"></param>
        /// <returns></returns>
        public int DelSheepCondition(int SheepConditionId)
        {
            var list = repository.GetValue(SheepConditionId);
            list.IsDel = 1;
            return repository.Update(list);
        }

        /// <summary>
        /// 反填品相测定
        /// </summary>
        /// <param name="SheepConditionId"></param>
        /// <returns></returns>
        public SheepCondition GetFtSheepCondition(int SheepConditionId)
        {
            return repository.GetValue(SheepConditionId);
        }

        /// <summary>
        /// 获取品相测定列表
        /// </summary>
        /// <param name="overBit"></param>
        /// <param name="SheepType"></param>
        /// <param name="SheepConditionSex"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<PageOutput<SheepCondition>> GetSheepCondition(string? overBit, int SheepType, int SheepConditionSex, int pageIndex, int pageSize)
        {
            var list = repository.GetAll().Where(c => c.IsDel == 0).ToList();
            //给我判断耳号
            if (!string.IsNullOrEmpty(overBit))
            {
                list = list.Where(c => c.OverBit == overBit).ToList();
            }
            if (SheepType > 0)
            {
                list = list.Where(c => c.SheepType == SheepType).ToList();
            }

            //给我判断 羊性别
            if (SheepConditionSex > 0)
            {
                bool boolSheepConditionSex = SheepConditionSex == 1 ? true : false;
                list = list.Where(c => c.SheepConditionSex == boolSheepConditionSex).ToList();
            }
         
            var result = new PageOutput<SheepCondition>()
            {
                Total = list.Count(),
                PageSize = pageSize,
            };
            var entity = list.OrderByDescending(c => c.SheepConditionId).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            result.Data = entity;
            return result;
        }

        /// <summary>
        /// 更新品相测定
        /// </summary>
        /// <param name="sheepCondition"></param>
        /// <returns></returns>
        public async Task<int> UpdaSheepCondition(SheepCondition sheepCondition)
        {
            return repository.Update(sheepCondition);
        }
    }
}
